package cn.autumnorange.app.snowflake.api;

import cn.autumnorange.app.common.dto.ResultDto;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class SnowFlakeFallbackFactory implements FallbackFactory<SnowFlakeService> {

  @Override
  public SnowFlakeService create(Throwable throwable) {
    return new SnowFlakeService() {
      @Override
      public ResponseEntity<ResultDto<Long>> getGlobalId() {
        ResponseEntity<ResultDto<Long>> result =
            ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
                .body(
                    ResultDto.createResult(
                        HttpStatus.INTERNAL_SERVER_ERROR.value(),
                        null,
                        throwable.getMessage(),
                        null,
                        null,
                        null));
        log.info("SnowFlakeFallbackFactory exe");
        return result;
      }

      @Override
      public ResponseEntity<ResultDto> test(HttpServletRequest request) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
            .body(ResultDto.createMessageResult(throwable.getMessage()));
      }
    };
  }
}
